package ctu.edu.vn.musicmanager.business;

import java.awt.Color;
import java.io.IOException;
import java.net.URL;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;

import ctu.edu.vn.musicmanager.gui.MusicManager;

public class PlayMusic extends Thread implements Runnable {

	private boolean playing;
	private AudioInputStream din;
	private SourceDataLine line;
	private int nBytesRead;
	private byte[] data;
	private JButton btnPlay;
	private JLabel lblSongName;
	private String SongName;
	public static ImageIcon PlayIcon = new ImageIcon("./icons/play.png"),
			StopIcon = new ImageIcon("./icons/stop.png");

	public PlayMusic(JButton btn, JLabel lbl) {
		super(new Thread());
		btnPlay = btn;
		lblSongName = lbl;
	}

	@Override
	public void run() {
		if (!playing)
			return;
		super.run();
		displayStop();
		try {
			while ((nBytesRead = din.read(data, 0, data.length)) != -1
					&& playing) {
				line.write(data, 0, nBytesRead);
			}
			if (din != null)
				din.close();
			if (line != null)
				line.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		displayPlay();
		playing = false;
		this.interrupt();
	}

	public boolean setMusic(int id, String songname) {
		this.SongName = songname;
		try {
			din = null;
			line = null;
			URL url = new URL("http://" + MusicManager.host + "/mymusic/" + id
					+ ".mp3");

			AudioInputStream in = AudioSystem.getAudioInputStream(url);
			AudioFormat baseFormat = in.getFormat();
			AudioFormat decodedFormat = new AudioFormat(
					AudioFormat.Encoding.PCM_SIGNED,
					baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
					baseFormat.getChannels() * 2, baseFormat.getSampleRate(),
					false);
			din = AudioSystem.getAudioInputStream(decodedFormat, in);
			DataLine.Info info = new DataLine.Info(SourceDataLine.class,
					decodedFormat);
			line = (SourceDataLine) AudioSystem.getLine(info);
			if (line != null) {
				line.open(decodedFormat);
				line.start();
				data = new byte[4096];
				playing = true;
			}

		} catch (IOException | LineUnavailableException
				| UnsupportedAudioFileException e) {
			return false;
		}
		return true;
	}

	public void stopMusic() {
		if (!playing) {
			return;
		}
		playing = false;
		displayPlay();
		this.interrupt();
	}

	private void displayPlay() {
		btnPlay.setIcon(PlayIcon);
		btnPlay.setText("Play");
		lblSongName.setText("Chưa chọn bài hát");
		lblSongName.setForeground(Color.BLACK);
	}

	private void displayStop() {
		btnPlay.setIcon(StopIcon);
		btnPlay.setText("Stop");
		lblSongName.setText(SongName);
		lblSongName.setForeground(Color.BLUE);
	}
}
